﻿using AgorApp.Roma.Model;
using AgorApp.Roma.Model.DataTransferObjects;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace AgorApp.Roma.Oracle.Services.Mappers
{
    public class AgorAppModelProfile : Profile
    {
        public override string ProfileName
        {
            get
            {
                return this.GetType().Name;
            }
        }

        protected override void Configure()
        {
            base.Configure();

            SourceMemberNamingConvention = new PascalCaseNamingConvention();
            DestinationMemberNamingConvention = new UpperUnderscoreNamingConvention();

            CreateMap<AssembleeCapitoline, ASSEMBLEE_CAPITOLINE>()
                .ForMember(dest => dest.TESTO_ODG, opt => opt.ResolveUsing((AssembleeCapitoline ass) =>
                {
                    return JsonConvert.SerializeObject(ass.TestoOdG);
                }));

            CreateMap<ConsiglieriAssemblea, CONSIGLIERI_ASSEMBLEA>()
                .ForMember(dest => dest.NOTE, opt => opt.ResolveUsing((ConsiglieriAssemblea cons) =>
                {
                    return JsonConvert.SerializeObject(cons.Note);
                }));

            CreateMap<ConsiglieriImages, CONSIGLIERI_IMAGES>()
                .ForMember(dest => dest.CONSIGLIERI_ASSEMBLEA, opt => opt.MapFrom(src => src.Consigliere));

            CreateMap<DelibereAssembleaCapitolina, DELIBERE_ASSEMBLEA_CAPITOLINA>()
                .ForMember(dest => dest.TESTO, opt => opt.ResolveUsing((DelibereAssembleaCapitolina del) =>
                {
                    return JsonConvert.SerializeObject(del.Testo);
                }));

            CreateMap<DelibereAssembleaFile, DELIBERE_ASSEMBLEA_FILE>();

            CreateMap<GruppiConsiliari, GRUPPI_CONSILIARI>()
                .ForMember(dest => dest.NOTE, opt => opt.ResolveUsing((GruppiConsiliari gr) =>
                {
                    return JsonConvert.SerializeObject(gr.Note);
                }));

            CreateMap<GruppiConsiliariImages, GRUPPI_CONSILIARI_IMAGES>();

            CreateMap<News, NEWS>()
                .ForMember(dest => dest.TESTO, opt => opt.ResolveUsing((News news) =>
                {
                    return JsonConvert.SerializeObject(news.Testo);
                }));

            CreateMap<NewsImages, NEWS_IMAGES>();

            CreateMap<Tag, TAG>()
                .ForMember(dest => dest.TAG1, opt => opt.MapFrom(src => src.TagName))
                .ForMember(dest => dest.ID_TIPO_TAG, opt => opt.ResolveUsing((Tag tag) =>
                {
                    return (short)tag.TagType;
                }))
                .ForMember(dest => dest.TAG_TYPE, opt => opt.Ignore());
        }
    }
}
